package com.stylefeng.guns.modular.vpnuserinfo.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.vpn.dto.UserQueryDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.VpnUserInfo;
import com.stylefeng.guns.modular.vpnuserinfo.service.IVpnUserInfoService;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户管理控制器
 *
 * @author fengshuonan
 * @Date 2020-03-02 16:15:44
 */
@Controller
@RequestMapping("/vpnUserInfo")
public class VpnUserInfoController extends BaseController {

    private String PREFIX = "/vpnuserinfo/vpnUserInfo/";

    @Autowired
    private IVpnUserInfoService vpnUserInfoService;

    /**
     * 跳转到用户管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vpnUserInfo.html";
    }

    /**
     * 跳转到添加用户管理
     */
    @RequestMapping("/vpnUserInfo_add")
    public String vpnUserInfoAdd() {
        return PREFIX + "vpnUserInfo_add.html";
    }

    /**
     * 跳转禁用账号
     * @return
     */
    @RequestMapping("/vpnUserInfo_disable")
    public String vpnUserInfoDisable() {
        return PREFIX + "vpnUserInfo_disable_user.html";
    }


    /**
     * 跳转到修改用户管理
     */
    @RequestMapping("/vpnUserInfo_update/{vpnUserInfoId}")
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public String vpnUserInfoUpdate(@PathVariable Integer vpnUserInfoId, Model model) {
//        VpnUserInfo vpnUserInfo = vpnUserInfoService.selectById(vpnUserInfoId);
//        model.addAttribute("item",vpnUserInfo);
//        LogObjectHolder.me().set(vpnUserInfo);
        return PREFIX + "vpnUserInfo_edit.html";
    }

    /**
     * 添加VIP套餐
     * @return
     */
    @RequestMapping("/add_vip_package")
    public String vipPackage() {
        return PREFIX + "add_vip_package.html";
    }

    /**
     * 获取用户管理列表
     */
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(UserQueryDTO userQueryDTO) {
        return vpnUserInfoService.findUserPage(userQueryDTO);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "userName",required = false) String userName,
                       @RequestParam(value = "beginTime",required = false) String beginTime,
                       @RequestParam(value = "endTime",required = false) String endTime,
                       @RequestParam(value = "packageId",required = false) String packageId) {
        return vpnUserInfoService.findAllUsers(userName,beginTime,endTime,packageId);
    }

    /**
     * 新增用户管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object add(VpnUserInfo vpnUserInfo) {
        vpnUserInfoService.insert(vpnUserInfo);
        return SUCCESS_TIP;
    }

    /**
     * 删除用户管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object delete(@RequestParam Integer vpnUserInfoId) {
        vpnUserInfoService.deleteById(vpnUserInfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改用户管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object update(VpnUserInfo vpnUserInfo) {
        vpnUserInfoService.updateById(vpnUserInfo);
        return SUCCESS_TIP;
    }

    /**
     * 用户管理详情
     */
    @RequestMapping(value = "/detail/{vpnUserInfoId}")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object detail(@PathVariable("vpnUserInfoId") Integer vpnUserInfoId) {
        return vpnUserInfoService.selectById(vpnUserInfoId);
    }

    @RequestMapping("/getFilterData")
    @ResponseBody
    public Object getSelectData(){
        return vpnUserInfoService.getFilterData();
    }

    @RequestMapping("/download/excel")
    @ResponseBody
    public void downloadOrderInfo(@RequestParam(value = "userName",required = false) String userName,
                                  @RequestParam(value = "beginTime",required = false) String beginTime,
                                  @RequestParam(value = "endTime",required = false) String endTime,
                                  @RequestParam(value = "packageName",required = false) String packageName,
                                  HttpServletResponse response){
        vpnUserInfoService.exportExcel(userName,beginTime,endTime,packageName,response);
    }

    /**
     * 重置用户密码
     */
    @RequestMapping(value = "/resetPwd")
    @ResponseBody
    public Object resetPwd(@RequestParam(value = "username") String userName,
                           @RequestParam(value = "pwd") String pwd) {
        vpnUserInfoService.resetPwd(userName,pwd);
        return SUCCESS_TIP;
    }

    /**
     * 禁用用户账号
     * @param userName
     * @param status 0- 禁用，1- 正常
     * @return
     */
    @RequestMapping(value = "/disableUser")
    @ResponseBody
    public Object disableUser(@RequestParam(value = "username") String userName,
                           @RequestParam(value = "status") Integer status) {
        vpnUserInfoService.disableUser(userName,status);
        return SUCCESS_TIP;
    }

    /**
     * 手动添加一个用户的套餐
     */
    @RequestMapping(value = "/addVipPackage")
    @ResponseBody
    public Object addVipPackage(@RequestParam(value = "username") String userName,
                           @RequestParam(value = "packageId") String packageId) {
        vpnUserInfoService.addVipPackage(userName,packageId);
        return SUCCESS_TIP;
    }
}
